#include "PointSet.h"
#include <iostream>

PointSet::PointSet()
{
	point = NULL;
	normal = NULL;
	quad = NULL;
	point_N = 0;
  storage_N = 0;
  bIsQuad = false;
}

PointSet::~PointSet()
{
	if(point != NULL)
		delete[] point;

	if(normal != NULL)
		delete[] normal;

	if(quad != NULL)
  {
		delete[] quad;
		delete[] tangent1;
		delete[] tangent2;
	}
}

void PointSet::allocateStorage(int n)
{
	point = new float[n][3];
	normal = new float[n][3];
	quad = new float[n][3];
	tangent1 = new float[n][3];
	tangent2 = new float[n][3];
  storage_N = n;
}

bool PointSet::isInPointSet(float x, float y, float z)
{
	for(int i = 0; i < point_N; ++i)
  {
		if(!equal(point[i][0], x))
      continue;
		if(!equal(point[i][1], y))
      continue;
		if(!equal(point[i][2], z))
      continue;
    return true;
	}
  return false;
}

bool PointSet::addPoint(float x, float y, float z, float nx, float ny, float nz)
{
  if(storage_N <= point_N)
    return false;
  else if(isInPointSet(x,y,z))
    return false;
  else
  {
	  point[point_N][0] = x;
	  point[point_N][1] = y;
	  point[point_N][2] = z;
	  normal[point_N][0] = nx;
	  normal[point_N][1] = ny;
	  normal[point_N][2] = nz;
    point_N++;
    return true;
  }
}

void PointSet::getBound(float &xmin, float &xmax, 
						 float &ymin, float &ymax, 
						 float &zmin, float &zmax) const
{
	xmin = point[0][0];
	xmax = point[0][0];
	ymin = point[0][1];
	ymax = point[0][1];
	zmin = point[0][2];
	zmax = point[0][2];

	for(int i=1; i<point_N; i++){
		if(point[i][0] > xmax)
			xmax = point[i][0];
		if(point[i][0] < xmin)
			xmin = point[i][0];

		if(point[i][1] > ymax)
			ymax = point[i][1];
		if(point[i][1] < ymin)
			ymin = point[i][1];

		if(point[i][2] > zmax)
			zmax = point[i][2];
		if(point[i][2] < zmin)
			zmin = point[i][2];
	}
}
